package com.sttri.wardrobe;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class welcomePage extends FragmentActivity{
	
	private ViewPager mPager;
	
	private PagerAdapter mAdapter;
	
	public final static Integer[] picIds = new Integer[] {
		R.drawable.guide1, R.drawable.guide2, R.drawable.guide3, R.drawable.empty
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcomepage);
		
		mAdapter = new welcomeAdapter(getSupportFragmentManager(), picIds.length);
		
		mPager = (ViewPager)findViewById(R.id.welcomeView);
		mPager.setAdapter(mAdapter);
		mPager.setOnPageChangeListener(new OnPageChangeListener(){

			private ImageView imageDot;
			private LinearLayout guideDotLayout = (LinearLayout) findViewById(R.id.guideDot);
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				for (int i=0; i < guideDotLayout.getChildCount(); i++){
				      ImageView v = (ImageView)guideDotLayout.getChildAt(i);
				     v.setImageResource(R.drawable.dot_black);
				}
				switch (arg0){
				case 0:
					imageDot=(ImageView)findViewById(R.id.guideDot1);
					imageDot.setImageResource(R.drawable.dot_red);
					break;
				case 1:
					imageDot=(ImageView)findViewById(R.id.guideDot2);
					imageDot.setImageResource(R.drawable.dot_red);
					break;
				case 2:
					imageDot=(ImageView)findViewById(R.id.guideDot3);
					imageDot.setImageResource(R.drawable.dot_red);
					break;
				
				}
				if (arg0 == 3){
				
					Intent callMain = new Intent(welcomePage.this, MainActivity.class);
					startActivity(callMain);
					welcomePage.this.finish();
				}
				
				
			}

		});

		
	

	}
	

	
	
	public static class welcomeAdapter extends FragmentPagerAdapter {

		private int mSize;
		
		public welcomeAdapter(FragmentManager fm, int size) {
			super(fm);
			// TODO Auto-generated constructor stub
			mSize = size;
		}

		@Override
		public Fragment getItem(int position) {
			// TODO Auto-generated method stub
			imageFrag f = imageFrag.newInstance(position);
			return f;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mSize;
		}
		
		
	


	}
	
	public static class imageFrag extends Fragment{
		
		private int mNum;
		private ImageView guidePic;
		
		static imageFrag newInstance(int num){
			imageFrag f = new imageFrag();
			Bundle args = new Bundle();
			args.putInt("num",num);
			f.setArguments(args);
			return f;
		}
		
				
		@Override
		public void onCreate(Bundle savedInstanceState) {
	            super.onCreate(savedInstanceState);
	            mNum = getArguments().getInt("num");
		  }

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
			View v = inflater.inflate(R.layout.fragment_guide,container, false);
			guidePic = (ImageView)v.findViewById(R.id.guidePic);	    
			return v;
		
		}
		
		@Override
	    public void onActivityCreated(Bundle savedInstanceState) {
	        super.onActivityCreated(savedInstanceState);
	        final int num = welcomePage.picIds[mNum];
	        guidePic.setImageResource(num);
	        // Load image into ImageView
	    }

		
	}

	

}
